package com.jpx.jxcbiz.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jpx.common.dto.TSupplierDto;
import com.jpx.jxc.common.vo.ResultEntity;
import com.jpx.jxcbiz.entity.TSupplier;
import com.jpx.jxcbiz.service.ITSupplierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/biz/tSupplier")
public class TSupplierController {

    @Autowired
    ITSupplierService supplierService;

    @RequestMapping("/list")
    public ResultEntity list(TSupplierDto supplierDto){
        PageHelper.startPage(supplierDto.getPageNo(),supplierDto.getPageSize());
        TSupplier supplier = new TSupplier();
        EntityWrapper<TSupplier> entityWrapper = new EntityWrapper<>(supplier);
        if(StringUtils.isNotBlank(supplierDto.getName())){
            entityWrapper.like("name",supplierDto.getName());
        }

        List<TSupplier> list = supplierService.selectList(entityWrapper);
        PageInfo<TSupplier> pageInfo = new PageInfo<>(list);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("/insert")
    public ResultEntity add(TSupplierDto supplierDto){
        TSupplier supplier = new TSupplier();
        BeanUtils.copyProperties(supplierDto,supplier);
        boolean b = supplierService.insert(supplier);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/update")
    public ResultEntity update(TSupplierDto supplierDto){
        TSupplier supplier = new TSupplier();
        BeanUtils.copyProperties(supplierDto,supplier);
        boolean b = supplierService.updateById(supplier);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/deleteByIds")
    public ResultEntity delete(String ids){
        int i = supplierService.deleteByIds(ids);
        return ResultEntity.ok(i);
    }

}
